<?php

namespace app\model;

use think\Model;
use app\model\ProjectOpenSource;

class ProjectOpenSourceType extends Model
{
    protected $table = "ea_project_open_source_type";

    public static function setWhere($data)
    {
        $model = self::whereNull('delete_time')->where('status', 1)->order('sort ASC');

        return $model;
    }

    /**
     * [getProjectTypeList 获取开源分类列表]
     * @since  2022-03-25
     * @author Dai哥
     * @param  array      $data [description]
     * @return [type]           [description]
     */
    public static function getProjectTypeList($data=[])
    {
        $list = self::setWhere($data)->field('id, title, name, pic, pic_big, remark')->withCount('source')->select();

        return $list;
    }

    /**
     * [source 关联查询]
     * @since  2022-03-25
     * @author Dai哥
     * @return [type]     [description]
     */
    public function source()
    {
        return $this->hasMany(ProjectOpenSource::class, 'type_id', 'id')->where('status', 1)->whereNull('delete_time');
    }

    /**
     * [getOneTypeInfo 获取指定专题的详细，以最新的5条记录]
     * @since  2022-03-25
     * @author Dai哥
     * @param  [type]     $data [description]
     * @return [type]           [description]
     */
    public static function getOneTypeInfo($data)
    {
        $info = self::setWhere($data)
            ->field('id, title, name, remark, pic, pic_big')
            ->withCount('source')
            ->with([
                'source' => function($query){
                    $query->order('id DESC')->limit(5);
                }
            ])
            ->find($data['id']);

        return $info;
    }
}